package com.ai.doc.service;

import com.ai.doc.util.DocUtil;
import com.ai.doc.util.EchartsUtil;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DocService implements ApplicationRunner {
    @Value("${path.phantomjs}")
    String phantomjsPath;

    @Value("${path.echartsConvert}")
    String EchartsConvertPath;

    @Value("${path.file}")
    String filePath;

    @Value("${isLinux}")
    Boolean isLinux;

    public void doc1() throws IOException {
        DocUtil.createDoc("doc.ftl", getDataMap(1), filePath + "t1.doc");
    }

    public void doc2() throws IOException {
        DocUtil.createDoc("doc.ftl", getDataMap(2), filePath + "t2.doc");
    }

    public void doc3() throws IOException {
        DocUtil.createDoc("doc.ftl", getDataMap(3), filePath + "t3.doc");
    }

    public Map<String, Object> getDataMap(int type) {
        EchartsUtil eu = new EchartsUtil(phantomjsPath, EchartsConvertPath, filePath, isLinux, true);

        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("h1", "Java生成Doc文档，内含Echarts图表");
        dataMap.put("h1desc", "202109");
        dataMap.put("h2", "Echarts图表展示");

        List<Map<String, Object>> itemList = new ArrayList<>();
        getLineData(type, eu, itemList);
        getBarData(type, eu, itemList);
        getPieData(type, eu, itemList);
        getRadarData(type, eu, itemList);
        dataMap.put("itemList", itemList);

        return dataMap;
    }

    private void getLineData(int type, EchartsUtil eu, List<Map<String, Object>> itemList) {
        String option = "{" +
                "    color: ['#80FFA5', '#00DDFF', '#37A2FF', '#FF0087', '#FFBF00']," +
                "    legend: {" +
                "        data: ['Line 1', 'Line 2', 'Line 3', 'Line 4', 'Line 5']" +
                "    }," +
                "    grid: {" +
                "        left: '3%'," +
                "        right: '4%'," +
                "        bottom: '3%'," +
                "        containLabel: true" +
                "    }," +
                "    xAxis: [" +
                "        {" +
                "            type: 'category'," +
                "            boundaryGap: false," +
                "            data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']" +
                "        }" +
                "    ]," +
                "    yAxis: [" +
                "        {" +
                "            type: 'value'" +
                "        }" +
                "    ]," +
                "    series: [" +
                "        {" +
                "            name: 'Line 1'," +
                "            type: 'line'," +
                "            stack: '总量'," +
                "            smooth: true," +
                "            lineStyle: {" +
                "                width: 0" +
                "            }," +
                "            showSymbol: false," +
                "            areaStyle: {" +
                "                opacity: 0.8," +
                "            }," +
                "            emphasis: {" +
                "                focus: 'series'" +
                "            }," +
                "            data: [140, 232, 101, 264, 90, 340, 250]" +
                "        }," +
                "        {" +
                "            name: 'Line 2'," +
                "            type: 'line'," +
                "            stack: '总量'," +
                "            smooth: true," +
                "            lineStyle: {" +
                "                width: 0" +
                "            }," +
                "            showSymbol: false," +
                "            areaStyle: {" +
                "                opacity: 0.8," +
                "            }," +
                "            emphasis: {" +
                "                focus: 'series'" +
                "            }," +
                "            data: [120, 282, 111, 234, 220, 340, 310]" +
                "        }," +
                "        {" +
                "            name: 'Line 3'," +
                "            type: 'line'," +
                "            stack: '总量'," +
                "            smooth: true," +
                "            lineStyle: {" +
                "                width: 0" +
                "            }," +
                "            showSymbol: false," +
                "            areaStyle: {" +
                "                opacity: 0.8," +
                "            }," +
                "            emphasis: {" +
                "                focus: 'series'" +
                "            }," +
                "            data: [320, 132, 201, 334, 190, 130, 220]" +
                "        }," +
                "        {" +
                "            name: 'Line 4'," +
                "            type: 'line'," +
                "            stack: '总量'," +
                "            smooth: true," +
                "            lineStyle: {" +
                "                width: 0" +
                "            }," +
                "            showSymbol: false," +
                "            areaStyle: {" +
                "                opacity: 0.8," +
                "            }," +
                "            emphasis: {" +
                "                focus: 'series'" +
                "            }," +
                "            data: [220, 402, 231, 134, 190, 230, 120]" +
                "        }," +
                "        {" +
                "            name: 'Line 5'," +
                "            type: 'line'," +
                "            stack: '总量'," +
                "            smooth: true," +
                "            lineStyle: {" +
                "                width: 0" +
                "            }," +
                "            showSymbol: false," +
                "            label: {" +
                "                show: true," +
                "                position: 'top'" +
                "            }," +
                "            emphasis: {" +
                "                focus: 'series'" +
                "            }," +
                "            data: [220, 302, 181, 234, 210, 290, 150]" +
                "        }" +
                "    ]" +
                "}";
        JSONObject optionJson = JSONObject.parseObject(option);
        option = "\"" + optionJson.toJSONString().replaceAll("\"", "'") + "\"";
        String img = eu.generateEchart(type, option, 800, 400);
        Map<String, Object> m = new HashMap<>();
        m.put("h3", "折线图");
        m.put("img", img);
        m.put("text", option);
        itemList.add(m);
    }

    private void getBarData(int type, EchartsUtil eu, List<Map<String, Object>> itemList) {
        String option = "{" +
                "    xAxis: {" +
                "        type: 'category'," +
                "        data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']" +
                "    }," +
                "    yAxis: {" +
                "        type: 'value'" +
                "    }," +
                "    series: [{" +
                "        data: [120, 200, 150, 80, 70, 110, 130]," +
                "        type: 'bar'," +
                "        barWidth: 30," +
                "        showBackground: true," +
                "        itemStyle: {" +
                "            normal: {" +
                "                color: '#FF0087'," +
                "                label: {" +
                "                    show: true," +
                "                    position: 'top'," +
                "                    formatter: '{c}'" +
                "                }" +
                "            }" +
                "        }," +
                "        backgroundStyle: {" +
                "            color: 'rgba(180, 180, 180, 0.2)'" +
                "        }" +
                "    }]" +
                "}";

        JSONObject optionJson = JSONObject.parseObject(option);
        option = "\"" + optionJson.toJSONString().replaceAll("\"", "'") + "\"";
        String img = eu.generateEchart(type, option, 800, 400);
        Map<String, Object> m = new HashMap<>();
        m.put("h3", "柱状图");
        m.put("img", img);
        m.put("text", option);
        itemList.add(m);
    }

    private void getPieData(int type, EchartsUtil eu, List<Map<String, Object>> itemList) {
        String option = "{" +
                "    backgroundColor: '#2c343c'," +
                "    series: [" +
                "        {" +
                "            name: '访问来源'," +
                "            type: 'pie'," +
                "            radius: '55%'," +
                "            center: ['50%', '50%']," +
                "            data: [" +
                "                {value: 335, name: '直接访问'}," +
                "                {value: 310, name: '邮件营销'}," +
                "                {value: 274, name: '联盟广告'}," +
                "                {value: 235, name: '视频广告'}," +
                "                {value: 400, name: '搜索引擎'}" +
                "            ]," +
                "            roseType: 'radius'," +
                "            label: {" +
                "                color: 'rgba(255, 255, 255, 0.3)'" +
                "            }," +
                "            labelLine: {" +
                "                lineStyle: {" +
                "                    color: 'rgba(255, 255, 255, 0.3)'" +
                "                }," +
                "                smooth: 0.2," +
                "                length: 10," +
                "                length2: 20" +
                "            }," +
                "            itemStyle: {" +
                "                color: '#c23531'," +
                "                shadowBlur: 200," +
                "                shadowColor: 'rgba(0, 0, 0, 0.5)'" +
                "            }," +
                "        }" +
                "    ]" +
                "}";

        JSONObject optionJson = JSONObject.parseObject(option);
        option = "\"" + optionJson.toJSONString().replaceAll("\"", "'") + "\"";
        String img = eu.generateEchart(type, option, 800, 400);
        Map<String, Object> m = new HashMap<>();
        m.put("h3", "饼图");
        m.put("img", img);
        m.put("text", option);
        itemList.add(m);
    }

    private void getRadarData(int type, EchartsUtil eu, List<Map<String, Object>> itemList) {
        String option = "{" +
                "    title: {" +
                "        text: '基础雷达图'" +
                "    }," +
                "    legend: {" +
                "        data: ['预算分配（Allocated Budget）', '实际开销（Actual Spending）']" +
                "    }," +
                "    radar: {" +
                "        indicator: [" +
                "            { name: '销售（Sales）', max: 6500}," +
                "            { name: '管理（Administration）', max: 16000}," +
                "            { name: '信息技术（Information Technology）', max: 30000}," +
                "            { name: '客服（Customer Support）', max: 38000}," +
                "            { name: '研发（Development）', max: 52000}," +
                "            { name: '市场（Marketing）', max: 25000}" +
                "        ]" +
                "    }," +
                "    series: [{" +
                "        name: '预算 vs 开销（Budget vs spending）'," +
                "        type: 'radar'," +
                "        data: [" +
                "            {" +
                "                value: [4200, 3000, 20000, 35000, 50000, 18000]," +
                "                name: '预算分配（Allocated Budget）'" +
                "            }," +
                "            {" +
                "                value: [5000, 14000, 28000, 26000, 42000, 21000]," +
                "                name: '实际开销（Actual Spending）'" +
                "            }" +
                "        ]" +
                "    }]" +
                "}";

        JSONObject optionJson = JSONObject.parseObject(option);
        option = "\"" + optionJson.toJSONString().replaceAll("\"", "'") + "\"";
        String img = eu.generateEchart(type, option, 800, 400);
        Map<String, Object> m = new HashMap<>();
        m.put("h3", "雷达图");
        m.put("img", img);
        m.put("text", option);
        itemList.add(m);
    }


    @Override
    public void run(ApplicationArguments args) throws Exception {
        doc1();
        doc2();
        doc3();
    }
}
